let Comment = require('../models/comment')
let Article = require('../models/article')

/**
 * 添加评论
 */
const add = async ctx => {
    const comment = ctx.request.body
    let isComment = false
    await Comment.create(comment).then(rel => {
        if (rel) {
            isComment = true
            ctx.body = {
                code: 200,
                msg: "评论发布成功"
            }
        } else {
            ctx.body = {
                code: 300,
                msg: "评论发布失败"
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            msg: "评论发布异常"
        }
        console.log(err);
    })
    if (isComment) {
        await Article.updateOne({ id: comment.id }, { $inc: { comment: 1 } })
    }
}

/**
 * 前台评论查询，通过文章ID查询
 */
const findByid = async ctx => {
    let { id } = ctx.query
    await Comment.find({ artileId: id }).then(rel => {
        if (rel && rel.length > 0) {
            ctx.body = {
                code: 200,
                msg: "评论查询成功",
                result: rel
            }
        } else {
            ctx.body = {
                code: 300,
                msg: "评论查询失败"
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            msg: "评论查询异常"
        }
        console.log(err);
    })
}
/**
 * 后台评论查询，通过文章作者查询
 */
const findByAuthor = async ctx => {
    let { page, author } = ctx.query
    //判断页码
    if (!page || isNaN(Number(page))) {
        page = 1
    } else {
        page = Number(page)
    }
    //每页条数
    let pageSize = 10
    //计算总页数
    let count = 0 //文章总条数
    await Article.find({ author }).count().then(rel => {
        count = rel
    })
    let totalPage = 0 //文章总页数
    if (count > 0) {
        totalPage = Math.ceil(count / pageSize)
    }

    //判断当前页码范围
    if (totalPage > 0 && page > totalPage) {
        page = totalPage
    } else if (page < 1) {
        page = 1
    }
    //计算起始位置
    let start = (page - 1) * pageSize
    await Comment.find({ author }).skip(start).limit(pageSize).then(rel => {
        if (rel && rel.length > 0) {
            ctx.body = {
                code: 200,
                msg: "评论查询成功",
                result: rel
            }
        } else {
            ctx.body = {
                code: 300,
                msg: "评论查询失败"
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            msg: "评论查询异常"
        }
        console.log(err);
    })
}
/**
 * 根据_id删除评论
 */
const commentRemove = async ctx => {
    let { _id } = ctx.query
    await Comment.findOneAndDelete({ _id }).then(rel => {
        if (rel) {
            ctx.body = {
                code: 200,
                msg: "评论删除成功"
            }
        } else {
            ctx.body = {
                code: 300,
                msg: "评论删除失败"
            }
        }
    }).catch(err => {
        ctx.body = {
            code: 500,
            msg: "评论删除异常",
        }
        console.log(err);
    })
}
module.exports = {
    add,
    findByid,
    findByAuthor,
    commentRemove
}
